/**
 * @param {string} ransomNote
 * @param {string} magazine
 * @return {boolean}
 */
var canConstruct = function(ransomNote, magazine) {
    let arr1 = ransomNote.split('').sort()
    let arr2 = magazine.split('').sort()
    let i = 0;
    let j = 0;
    while(i < arr1.length && j < arr2.length) {
        if(arr1[i] == arr2[j]) {
            i++
            j++
        } else {
            j++
        }
    }
    if(i == arr1.length) return true
    return false
};
// 哈希表
var canConstruct1 = function(ransomNote, magazine) {
    if(ransomNote.length > magazine.length) {
        return false
    }
    let m1 = new Map(), m2 = new Map()
    let i = 0
    for(;i<ransomNote.length; i++) {
        let r = ransomNote.charAt(i)
        let m = magazine.charAt(i)
        if(m1.has(r)) m1.set(r, m1.get(r) + 1)
        else m1.set(r, 1)
        if(m2.has(m)) m2.set(m, m2.get(m) + 1)
        else m2.set(m, 1)
    }
    for(;i<magazine.length; i++) {
        let m = magazine.charAt(i)
        if(m2.has(m)) m2.set(m, m2.get(m) + 1)
        else m2.set(m, 1)
    }
    for(let entry of m1.entries()) {
        if(!m2.has(entry[0]) || m2.get(entry[0]) < entry[1]) return false
    }
    return true
};
console.log(canConstruct("a", "b"));
console.log(canConstruct("aa", "ab"));
console.log(canConstruct("aa", "aab"));